package racing.rule;

import javax.swing.ImageIcon;

/**
 * RPS rule. (Rock, Paper, Scissors)
 * 
 * @author Sarathit Sangtaweep 5510546182
 * @author Veerapat Teeravipark 5510547022
 * @version 2013
 */
public class RPSRule implements Rules{

	@Override
	public Rules getRule() {
		return this;
	}
	@Override
	public String toString() {
		return "RPS Rule";	
	}
		
	/**
	 * Get the image of the RPS.
	 * @param img the RPS's name.
	 * @return the image of the RPS's name.
	 */
	public ImageIcon getImageIcon(String img){
		if(img.equals("Rock")) return new ImageIcon(this.getClass().getResource("/images/rock.png"));
		else if(img.equals("Paper")) return new ImageIcon(this.getClass().getResource("/images/paper.png"));
		else return new ImageIcon(this.getClass().getResource("/images/scissors.png"));
	}
	
	/**
	 * Decide the winner by comparing two RPS.
	 * @param p1 RPS from the first user.
	 * @param p2 RPS from the second user.
	 * @return the RPS of the winner.
	 */
	public RPS play(RPS p1, RPS p2){
		if(p1 == RPS.ROCK && p2 == RPS.PAPER) return p2;
		else if(p1 == RPS.ROCK && p2 == RPS.SCISSORS) return p1;
		else if(p1 == RPS.PAPER && p2 == RPS.ROCK) return p1;
		else if(p1 == RPS.PAPER && p2 == RPS.SCISSORS) return p2;
		else if(p1 == RPS.SCISSORS && p2 == RPS.PAPER) return p1;
		else if(p1 == RPS.SCISSORS && p2 == RPS.ROCK) return p2;
		return RPS.DRAW;
	}

}
